#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

function installPkg {
    clear
    read -p "Continue with the reinstallation? [Y/n] " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        if [[ ! -d /etc/squid ]]; then
            wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/squid.sh | bash
        fi
        apt-get -y -qq --purge remove squid &>/dev/null
        apt-get -y -qq autoremove &>/dev/null
        apt-get -qq clean &>/dev/null
        clear; sleep 3
        wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/squid.sh | bash
    else
        echo
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
        echo
    fi
}

function uninstallPkg {
    clear
    read -r -p "Are you sure you want to uninstall squid? [Y/n] " __yesno
    if [[ $_yesno = ^[Yy]$ ]]; then
        apt-get -y -qq --purge remove squid &>/dev/null
        apt-get -qq autoremove &>/dev/null
        apt-get -qq autoclean &>/dev/null

        echo -e "${SUCCESS} SUCCESS ${RESET}${TEXT} Purge & Removing squid package ${RESET}"; exit 0
    else
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
    fi
}

function configurePkg {
    clear
    squid_ports=($(cat /etc/squid/squid.conf | grep -w 'http_port' | cut -d ' '  -f 2,4,6))

    echo
    echo -e "By default squid service use three port $squid_ports"
    echo
    while true; do
        read -r -p "Do you want to add new port [Y/n]? " __yesno
        if [[ "$__yesno" =~ ^[Yy]$ ]]; then
            read -p "Enter new ports: " first_port second_port third_port
            if [[ -n "$first_port" && "$first_port" =~ ^[0-9]+$ ]]; then
                sed -i 's|${squid_ports[0]}|$first_port|g' /etc/squid/squid.conf
            fi
            if [[ -n "$second_port" && "$second_port" =~ ^[0-9]+$ ]]; then
                sed -i 's|${squid_ports[1]}|$second_port|g' /etc/squid/squid.conf
            fi
            if [[ -n "$third_port" && "$third_port" =~ ^[0-9]+$ ]]; then
                sed -i 's|${squid_ports[2]}|$third_port|g' /etc/squid/squid.conf
            fi

            echo -e "${INFO} INFO ${RESET}${TEXT} Changes have been made ${RESET}"; break
        else
            echo; echo -e "${INFO} INFO ${RESET}${TEXT} No changes made ${RESET}"; break
        fi
    done
}

function detailPkg {
    clear
    local unit_id=$(systemctl show squid.service -p Id | cut -d '=' -f 2)
    local unit_name=$(systemctl show squid.service -p Names | cut -d '=' -f 2)
    local unit_desc=$(systemctl show squid.service -p Description | cut -d '=' -f 2)
    local is_active=$(systemctl is-active squid.service)
    local is_enable=$(systemctl is-enabled squid.service)
    local squid_ports=$(cat /etc/squid/squid.conf | grep -w 'http_port' | cut -d ' '  -f 2,4,6)

    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} Squid service detail                                     ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "${YELLOW}     ID:${RESET} ${GREEN}$unit_id${RESET}"
    echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
    echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
    echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
    echo -e "${YELLOW}  Ports:${RESET} ${GREEN}$squid_ports{RESET}";
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
}

function squidMenu {
    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} SQUID MENU                                                 ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "[01] ${GREEN}reinstall${RESET}  - ${YELLOW}Reinstall squid package${RESET}"
    echo -e "[02] ${GREEN}uninstall${RESET}  - ${YELLOW}Uninstall squid package${RESET}"
    echo -e "[03] ${GREEN}configure${RESET}  - ${YELLOW}Edit/Configure squid config${RESET}"
    echo -e "[04] ${GREEN}detail   ${RESET}  - ${YELLOW}Show squid detail & status${RESET}"
    echo -e "[00] ${GREEN}quit     ${RESET}  - ${YELLOW}Exit from menu${RESET}"
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Enter your choice: " _choice
        case "$_choice" in
            01|reinstall)
                installPkg && break
            ;;
            02|uninstall)
                uninstallPkg && break
            ;;
            03|configure)
                configurePkg && break
            ;;
            04|detail)
                detailPkg && break
            ;;
            00|quit)
                exit 0
            ;;
            *)
                echo -e "${ERROR} ERROR ${RESET}${TEXT} Invalid option! ${RESET}"; squidMenu
            ;;
        esac
    done
}

if [[ -f /etc/squid/squid.conf ]]; then
    squidMenu
elif [[ ! -f /etc/squid/squid.conf ]]; then
    installPkg
fi